package com.errorfinding.tabbitmqDemo.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;


/**
 * 配置类
 * TTL队列 配置文件类代码
 */
@Configuration
public class TtlQueueConfig {

    //普通交换机的名称
    private static final String x_exchange="x";
    //死信交换机的名称
    private static final String y_dead_letter_exchange="y";
    //普通队列的名称
    private static final String queue_a="qa";
    private static final String queue_b="qb";

    private static final String queue_c="qc";
    //死信队列的名称
    private static final String dead_letter_queue_d="qd";

    //声明xExchange交换机
    @Bean("xExchange")
    public DirectExchange xExchange(){
        return new DirectExchange(x_exchange);
    }

    //声明yExchange交换机
    @Bean("yExchange")
    public DirectExchange yExchange(){
        return new DirectExchange(y_dead_letter_exchange);
    }

    //声明普通队列
    @Bean("queueA")
    public Queue queueA(){
        Map<String,Object> arguments = new HashMap<>();
        //设置死信交换机
        arguments.put("x-dead-letter-exchange",y_dead_letter_exchange);
        //设置死信RoutingKey
        arguments.put("x-dead-letter-routing-key","yd");
        //设置TTL 过期时间 单位是 毫秒
        arguments.put("x-message-ttl",10000);
        return QueueBuilder.durable(queue_a).withArguments(arguments).build();
    }
    @Bean("queueB")
    public Queue queueB(){
        Map<String,Object> arguments = new HashMap<>();
        //设置死信交换机
        arguments.put("x-dead-letter-exchange",y_dead_letter_exchange);
        //设置死信RoutingKey
        arguments.put("x-dead-letter-routing-key","yd");
        //设置TTL 过期时间 单位是 毫秒
        arguments.put("x-message-ttl",40000);
        return QueueBuilder.durable(queue_b).withArguments(arguments).build();
    }

    @Bean("queueC")
    public Queue queueC(){
        Map<String,Object> arguments = new HashMap<>();
        //设置死信交换机
        arguments.put("x-dead-letter-exchange",y_dead_letter_exchange);
        //设置死信RoutingKey
        arguments.put("x-dead-letter-routing-key","yd");

        return QueueBuilder.durable(queue_c).withArguments(arguments).build();
    }

    //声明死信队列
    @Bean("queueD")
    public Queue queueD(){
        return QueueBuilder.durable(dead_letter_queue_d).build();
    }

    //绑定
    @Bean
    public Binding queueABindingX(@Qualifier("queueA") Queue queueA,
                                  @Qualifier("xExchange") DirectExchange xExchange){
        return BindingBuilder.bind(queueA).to(xExchange).with("xa");
    }

    @Bean
    public Binding queueBBindingX(@Qualifier("queueB") Queue queueB,
                                  @Qualifier("xExchange") DirectExchange xExchange){
        return BindingBuilder.bind(queueB).to(xExchange).with("xb");
    }

    @Bean
    public Binding queueCBindingX(@Qualifier("queueC") Queue queueC,
                                  @Qualifier("xExchange") DirectExchange xExchange){
        return BindingBuilder.bind(queueC).to(xExchange).with("xc");
    }

    @Bean
    public Binding queueDBindingY(@Qualifier("queueD") Queue queueD,
                                  @Qualifier("yExchange") DirectExchange yExchange){
        return BindingBuilder.bind(queueD).to(yExchange).with("yd");
    }


}
